﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class ReBoxWin : Form
    {
         cMethods.DBHelper db;
        string barCode = "";
        public ReBoxWin(string bCode)
        {
            db = new cMethods.DBHelper();                     
            barCode = bCode;
            InitializeComponent();
        }

        private void CloseBtnBox_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }


        private void ReBtnBox_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {

                getPrintPars();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        private void ReBoxWin_Load(object sender, EventArgs e)
        {
            try
            {
      
                this.MaximizeBox = false;
                this.Text = "条码打印窗体";
                tbBoxCode.Text = barCode;
                tbBoxCode.ReadOnly = true;
                loadPrintTemplte();
                PrintDocument reRintDocument = new PrintDocument(); //获取默认打印机的方法
                reBoxPrint.Text = reRintDocument.PrinterSettings.PrinterName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // <summary>
        // 打印参数
        // </summary>
        private void getPrintPars()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select barCode,proCodes,capacity from bar_ProductBoxCode_info");
            sb.Append(" where barCode='" + barCode + "' ");
            var table = db.getData(sb.ToString(), "");
           // table.Columns.Add(" ");           
            if (table.Rows.Count > 0)
            {
                table.Rows[0]["barCode"] = table.Rows[0]["barCode"].ToString();               
                table.Rows[0]["capacity"] = table.Rows[0]["capacity"].ToString();
            }
            
            
            DoPrint(table);
        }     


        private VBprinter.EasyReport easyPrint;
        /// <summary>
        /// 打印方法
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private bool DoPrint(DataTable dt)
        {
            easyPrint = new VBprinter.EasyReport();
            string currentDir = AppDomain.CurrentDomain.BaseDirectory;
            string configFile = Path.Combine(currentDir + "Template\\", reBoxTemplate.SelectedValue.ToString()) + ".mb";//路径拼接

            if (File.Exists(configFile))
            {

                easyPrint.SetReportDataSource(dt);
                easyPrint.SetReportFile(configFile);
                easyPrint.PrintReport(false, true, false, false, reBoxPrint.Text).ToString();

                return true;
            }
            else
            {
                MessageBox.Show("打印模板不存在，请先设计后在打印！");      //未选择模版
                return false;
            }
        }


        /// <summary>
        /// 打印模板
        /// </summary>
        private void loadPrintTemplte()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select lbCode,lbName from bar_PrintTemplates_list where status=1");
            //sb.Append(" and lType='" + planType + "'"); 
            var table = db.getData(sb.ToString(), "");
            reBoxTemplate.DataSource = table;
            reBoxTemplate.ValueMember = "lbCode";
            reBoxTemplate.DisplayMember = "lbName";
        }


        private void labelControl4_Click(object sender, EventArgs e)
        {

        }
    }
}
